﻿using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Extend;
using System;

namespace DevelopTool.Forms.CommonControls.CodeEditor
{
    /// <summary>
    /// 文本查找窗口
    /// </summary>
    public partial class CodeEditorQueryForm : BaseUIForm
    {
        private TextEditorControl textEditorControl;
        private TextEditorQueryExtend textEditorQueryExtend;

        private int count = 0;
        private int index = 0;

        public CodeEditorQueryForm()
        {

        }

        public CodeEditorQueryForm(TextEditorControl textEditorControl)
        {
            this.textEditorControl = textEditorControl;
            this.textEditorQueryExtend = new TextEditorQueryExtend(this.textEditorControl);
            InitializeComponent();
        }

        private void TextEditorQueryForm_Load(object sender, EventArgs e)
        {

            this.txtKeyWord.Text = TextEditorGlobalData.TextEditorQueryHistoryText;

            TextEditorGlobalData.IsShowQueryForm = true;

            this.count = 0;
            this.index = 0;

            ThemHelper.SetSystemThem(this);
        }

        /// <summary>
        /// 查找下一个
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQueryNext_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtKeyWord.Text))
            {
                Tuple<int, int> line = textEditorQueryExtend.TextPosition(this.txtKeyWord.Text, this.chbCirculate.Checked, this.chbIgnoreCase.Checked);

                if (this.count <= 0)
                    this.count = textEditorQueryExtend.GetTextCount(this.txtKeyWord.Text, this.chbIgnoreCase.Checked);

                this.index++;

                if (this.index > this.count) this.index = 1;
                if (this.count <= 0) this.index = 0;

                this.Text = $"查找（{this.count}/{this.index}）行{line.Item1} 列{line.Item2}";
            }
            else
            {
                this.Text = "查找";
            }
        }

        /// <summary>
        /// 计数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiSymbolButton1_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtKeyWord.Text))
            {
                int count = textEditorQueryExtend.GetTextCount(this.txtKeyWord.Text, this.chbIgnoreCase.Checked);
                this.Text = $"找到{count}个匹配项";
            }
            else
            {
                this.Text = "查找";
            }
        }


        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiSymbolButton2_Click(object sender, EventArgs e)
        {
            this.Close();
            TextEditorGlobalData.IsShowQueryForm = false;
        }


        private void txtKeyWord_TextChanged(object sender, EventArgs e)
        {
            TextEditorGlobalData.TextEditorQueryHistoryText = txtKeyWord.Text;//把每次输入的搜索关键字存起来，方便下次直接用
        }

        private void TextEditorQueryForm_Activated(object sender, EventArgs e)
        {
            try
            {
                this.Opacity = 1.0;
            }
            catch { }
        }

        private void TextEditorQueryForm_Deactivate(object sender, EventArgs e)
        {
            try
            {
                this.Opacity = 0.5;
            }
            catch { }

        }
    }
}
